﻿$(document).ready(function() {
    $("#cbRole").change(function() {
        var thisval = $(this).val();
        if (!(typeof thisval == 'undefined' || thisval == '0' || thisval == 0)) {
            var sendData = {
                "_RoleID": thisval
            }
            $.ajax({
                type: "POST",
                url: 'MenuAccess.aspx/LoadData',
                data: JSON.stringify(sendData),
                contentType: "application/json;charset=utf-8",
                datatype: "json",
                success: function(data) {
                    var result = data.d;
                    for (var idx = 1; idx < 13; idx++) {
                        if (!(idx == 1 || idx == 6)) {
                            var chkID = "chk" + idx;
                            var isChecked = (result[idx] == "") ? "" : true;
                            $("#" + chkID).prop("checked", isChecked);
                        }
                    }
                }
            });
            return false;
        } else {
            for (var idx = 1; idx < 13; idx++) {
                if (!(idx == 1 || idx == 6)) {
                    var chkID = "chk" + idx;
                    $("#" + chkID).prop("checked", false);
                }
            }
        }
    });

    $(".chks").change(function() {
        var totalChecked = 5;
        var countChecked = 0;
        var isTrue = 0;
        var isFalse = 0;
        for (var idx = 8; idx < 13; idx++) {
            var chkID = "chk" + idx;
            var chks = $("#" + chkID).prop("checked");
            if (chks == true) {
                isTrue++;
            } else {
                isFalse++;
            }
        }
        var tmp = (totalChecked + (isTrue - isFalse)) / 2;
        if (tmp == 0) {
            $("#chk2").val(0);
        } else {
            $("#chk2").val(2);
        }
    });

    $("#btnSave").click(function(e) {
        e.preventDefault();

        var arrData = [];
        for (var idx = 1; idx < 13; idx++) {
            var chkID = "chk" + idx;
            var chks = $("#" + chkID);
            if (!(idx == 2)) {
                if (chks.prop("checked")) {
                    arrData.push(chks.val());
                }
            } else if (idx == 2) {
                if (chks.val() == 2 || chks.val() == '2') {
                    arrData.push(chks.val());
                }
            }

        }
        var sendData = {
            "Role": $("#cbRole").val(),
            "ActiveMenu": arrData
        };
        $.ajax({
            type: "POST",
            url: 'MenuAccess.aspx/UpdateMenu',
            data: JSON.stringify(sendData),
            contentType: "application/json;charset=utf-8",
            datatype: "json",
            success: function(data) {
                var result = data.d;
                alert(result[1]);
                if (result[0] == 1) {
                    window.location.href = "MenuAccess.aspx";
                }
            }
        });
        return false;
    });

});